package com.burgeon.backend.EnterpriseWeChat.rest.vip;

import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetVipByPhoneAndCardNo extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public GetVipByPhoneAndCardNo(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public Vip getData(String content, String empId) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(content,empId), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Vip> vipList = new ArrayList<>();

        if(jsonArray.length()>0){
//            for (int i = 0; i < jsonArray.length(); i++) {
            Vip vip = new Vip();
                vip.setVipName(jsonArray.getJSONArray(0).getString(0));
                vip.setCardNo(jsonArray.getJSONArray(0).getString(1));
                vip.setLogoImg(jsonArray.getJSONArray(0).getString(2));
                vip.setOpenid(jsonArray.getJSONArray(0).getString(3));
                vip.setPhone(jsonArray.getJSONArray(0).getString(4));
                vip.setId(jsonArray.getJSONArray(0).getString(5));
                vip.setLastDate(jsonArray.getJSONArray(0).getString(6));
            if("是".equals(jsonArray.getJSONArray(0).getString(7))){
                vip.setUnread("Y");
            }else {
                vip.setUnread("N");
            }
            vip.setExternalUserId(jsonArray.getJSONArray(0).getString(8));

//            }

return vip;
        }
//        System.err.println(vipList.size());
        return null;
    }

    private MultiValueMap Data(String content,String empId) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("VIPNAME");//vip姓名
        colums.add("CARDNO");//卡号
        colums.add("LOGO_URL");//头像
//        colums.add("YC_8LABELDEFINE_ID");//累计购物金额
        colums.add("OPENID2");
        colums.add("MOBIL");
        colums.add("ID");
        colums.add("'FA_VIPACC_ID;LASTDATE'");
        colums.add("UNREAD");//	UNREAD未读消息
        colums.add("EXTERNALUSERID");//企业微信我的顾客标识

        params.put("columns", colums);
        params.put("table", "C_VIP");//表名
        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();

            expr1.put("column", "'CARDNO'");
            String b =  "'"+content+"'";
            expr1.put("condition", b);


        HashMap expr2 = new HashMap();
        String ac =  "'"+content+"'";
        expr2.put("column", "'MOBIL'");
        expr2.put("condition", ac);


        HashMap expr3 = new HashMap();
        String c =  "'"+empId+"'";
        expr3.put("column", "'HR_EMPLOYEE_ID1'");
        expr3.put("condition", c);

        HashMap expr4 = new HashMap();
        expr4.put("combine", "or");
        expr4.put("expr1", expr1);
        expr4.put("expr2", expr2);

        paramss.put("combine", "and");
        paramss.put("expr1", expr3);
        paramss.put("expr2", expr4);


        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetVipByPhoneAndCardNo g = new GetVipByPhoneAndCardNo("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {//15830987520
            System.err.println(g.getData("15830987520","718"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
